package cn.itrip.biz.controller;

import cn.itrip.beans.domain.ItripHotelOrder;
import cn.itrip.beans.domain.ItripHotelRoom;
import cn.itrip.beans.domain.ItripUser;
import cn.itrip.beans.dtos.Dto;
import cn.itrip.beans.vo.*;
import cn.itrip.biz.service.HotelOrderService;
import cn.itrip.biz.service.HotelRoomService;

import cn.itrip.common.DtoUtils;
import cn.itrip.common.TokenUtil;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/hotelorder")
public class HotelOrderController {
    @Resource
    private HotelOrderService hotelOrderService;
    @Resource
    private HotelRoomService hotelRoomService;
    @Resource
    private TokenUtil tokenUtil;
    @RequestMapping(value = "/getpreorderinfo", method = RequestMethod.POST)
    @ResponseBody
    public Dto getpreorderinfo(@RequestBody Map<String, Object> params){
        RoomStoreVo roomStoreVo = new RoomStoreVo();
        ItripHotelRoom hotelRoom = hotelRoomService.selectById(Long.parseLong((String) params.get("roomId")));
        roomStoreVo.setPrice(hotelRoom.getRoomPrice());
        roomStoreVo.setHotelName(hotelRoom.getHotelName());
        //设置最小库存
        List<StoreVO> storeVOList = hotelOrderService.getpreorderinfo(params);
        int count = (int)params.get("count");
        int minCount =storeVOList.get(0).getStore();
        roomStoreVo.setStore(minCount);
        if(minCount < count){
            return DtoUtils.returnFail("库存不足","30003");
        }
        params.put("store",minCount);
        return DtoUtils.returnSuccess("获取成功",roomStoreVo);
    }
    @RequestMapping(value = "/addhotelorder", method = RequestMethod.POST)
    @ResponseBody
    public Dto addhotelorder(@RequestBody ItripAddHotelOrderVO vo,HttpServletRequest request) throws Exception {
        String token = request.getHeader("token");
        ItripUser user = tokenUtil.getUser(token);
        if(user == null){
            return DtoUtils.returnFail("请先登录","10001");
        }
        //
        ItripHotelOrder order = new ItripHotelOrder();
        BeanUtils.copyProperties(vo,order);

            order.setUserId(user.getId());
            order.setCreatedBy(user.getId());
            //设置入住用户
        String linkUserName = vo.getLinkUser().stream().map(i->i.getLinkUserName()).collect(Collectors.joining(","));
        order.setLinkUserName(linkUserName);
            hotelOrderService.addhotelorder(order);

        Map<String,Object>data = new HashMap<>();
        data.put("orderNo",order.getOrderNo());
        data.put("id",order.getId());
        return DtoUtils.returnSuccess("生成订单成功",data);
    }
    @RequestMapping(value = "/getpersonalorderroominfo{orderId}", method = RequestMethod.POST)
    @ResponseBody
    public Dto<Object> getPersonalOrderRoomInfo(@ApiParam(required = true, name = "orderId", value = "订单ID")
                                                @PathVariable String orderId,
                                                HttpServletRequest request) {
        Dto<Object> dto = null;
        String token = request.getHeader("token");
        ItripUser user = tokenUtil.getUser(token);
        if (null != user) {
            if (null == orderId || "".equals(orderId)) {
                return DtoUtils.returnFail("请传递参数：orderId", "100529");
            }
            try {
                ItripPersonalOrderRoomVO vo = hotelOrderService.getItripHotelOrderRoomInfoById(Long.valueOf(orderId));
                if (null != vo) {
                    dto = DtoUtils.returnSuccess("获取个人订单房型信息成功", vo);
                } else {
                    dto = DtoUtils.returnFail("没有相关订单房型信息", "100530");
                }
            } catch (Exception e) {
                e.printStackTrace();
                dto = DtoUtils.returnFail("获取个人订单房型信息错误", "100531");
            }
        } else {
            dto = DtoUtils.returnFail("token失效，请重登录", "100000");
        }
        return dto;
    }
}
